package com.wanmait.recycle.webcontroller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.pojo.Article;
import com.wanmait.recycle.pojo.Comment;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.Rubbish;
import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.service.ArticleService;
import com.wanmait.recycle.service.CommentService;
import com.wanmait.recycle.service.FirsttypeService;
import com.wanmait.recycle.service.RubbishService;
import com.wanmait.recycle.service.UserinfoService;
import com.wanmait.recycle.util.IPUtil;
import com.wanmait.recycle.util.RemoveHtmlTagUtil;

@Controller
@RequestMapping("web/")
public class WebArticleController {

	@Resource
	private ArticleService articleService;
	@Resource
	private CommentService commentService;
	@Resource
	private RubbishService rubbishService;
	@Resource
	private FirsttypeService firsttypeService;
	@Resource
	private UserinfoService userinfoService;
	
	
	/*获得文章列表*/
	@RequestMapping("article/list")
	public ModelAndView articleList() {
		
		ModelAndView mv = new ModelAndView();
		List<Firsttype> firsttypes = firsttypeService.findAll();
		List<Firsttype> s1 = new ArrayList<>();
		for (Firsttype firsttype : firsttypes) {
			if (firsttype.getSecondtypes().size()>0) {
				s1.add(firsttype);
			}
		}
		mv.addObject("firsttypes", s1);
		PageInfo<Article> pageInfo = articleService.findEnableArticle();
		for (Article article : pageInfo.getList()) {
			article.setContent(RemoveHtmlTagUtil.htmlRemoveTag(article.getContent()));
		}
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}
	/*获得文章详情*/
	@RequestMapping("article/articleSingle")
	public ModelAndView article(Integer articleId,HttpSession session,HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		Article article = articleService.findByArticleId(articleId);
		PageInfo<Comment> pageInfo = commentService.findCommentByArticleId(articleId);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("article", article);
		//获得当前用户，判断当前用户对当前文章是否点赞过
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		boolean isPraise = false;		//是否点赞
		boolean isCollect = false;		//是否收藏
		boolean isViewArticle = false;	//是否浏览过，如果没有，则记录一次（如果浏览过，时间超过24小时，也记录一次）
		//获得该次请求的ip
		String ip = IPUtil.getIpAddr(request);
		isViewArticle = articleService.isViewArticle(articleId, ip);
		
		if(userinfo!=null) {		
			isPraise = articleService.isPraise(articleId, userinfo.getId());
			isCollect = articleService.isCollect(articleId, userinfo.getId());
		}
		mv.addObject("isPraise", isPraise);
		mv.addObject("isCollect", isCollect);
		if(isViewArticle) {
			articleService.addViewArticle(articleId, ip);
		}
		
		return mv;
		
	}
	
	/*根据文章Id获得评论*/
	@RequestMapping("article/comment")
	@ResponseBody
	public ModelAndView getComment(Integer articleId,Integer pageNum) {
		ModelAndView mv = new ModelAndView();
		PageInfo<Comment> pageInfo = commentService.findCommentByArticleId(articleId);
		mv.addObject("pageInfo", pageInfo);
		return mv;
		
	}
	/*添加一级评论*/
	@RequestMapping("article/addFir")
	public String addFirstComment(Comment comment) {
		commentService.addFirstComment(comment);
		articleService.updateCommentCount(comment.getArticle().getId(), 1);
		return "redirect:articleSingle?articleId="+comment.getArticle().getId();
	}
	/*添加二级评论*/
	@RequestMapping("article/addSec")
	@ResponseBody
	public void addSecondComment(Comment comment,Integer articleId,Integer userinfoId) {
		Userinfo userinfo = new Userinfo();
		userinfo.setId(userinfoId);
		Article article = new Article();
		article.setId(articleId);
		comment.setUserinfo(userinfo);
		comment.setArticle(article);
		
		commentService.addSecondComment(comment);
	}
	
	/*点赞*/
	@RequestMapping("article/addPraise")
	@ResponseBody
	public void addPraise(Integer articleId,HttpSession session) {
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		articleService.addPraise(articleId, userinfo.getId());
	}
	
	/*取消点赞*/
	@RequestMapping("article/removePraise")
	@ResponseBody
	public void removePraise(Integer articleId,HttpSession session) {
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		articleService.removePraise(articleId, userinfo.getId());
	}
	
	/*收藏*/
	@RequestMapping("article/addCollect")
	@ResponseBody
	public void addCollect(Integer articleId,HttpSession session) {
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		articleService.addCollect(articleId, userinfo.getId());
	}
	
	/*取消收藏*/
	@RequestMapping("article/removeCollect")
	@ResponseBody
	public void removeCollect(Integer articleId,HttpSession session) {
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		articleService.removeCollect(articleId, userinfo.getId());
	}
	
	
	/*添加文章*/
	@RequestMapping(value="article/addArticle",method=RequestMethod.GET)
	public ModelAndView addArticle(HttpSession session) {
		ModelAndView mv = new ModelAndView();
		Userinfo userinfo =  (Userinfo)session.getAttribute("userinfo");
		if(userinfo==null) {
			mv.addObject("msg", "login");
			mv.setViewName("redirect:/index.html");
		}
		return mv;
	}
	@RequestMapping(value="article/addArticle",method=RequestMethod.POST)
	public String subArticle(Article article,HttpSession session) {
		Userinfo userinfo =  (Userinfo)session.getAttribute("userinfo");
		articleService.addArticle(article);
		userinfo.setPublishCount(1);
		userinfoService.updateCount(userinfo);
		return "redirect:/web/article/list";
	}
	/*点击采用*/
	@RequestMapping(value="article/using",produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String using(Integer id,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		Rubbish rubbish = rubbishService.selectById(id);
		
		mv.addObject("rubbish", rubbish);
		if(rubbish!=null) {
			return rubbish.getName();
		}
		return "";
	}
	
	/*某个用户的文章*/
	@RequestMapping("userinfo/articleList")
	public ModelAndView findArticleByUserinfoId(HttpSession session){
		 
		ModelAndView mv = new ModelAndView();
		try {
			
			Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
			PageInfo<Article> pageInfo = articleService.findArticleByUserinfoId(userinfo.getId());
			
			mv.addObject("pageInfo", pageInfo);
		} catch (Exception e) {
			mv.addObject("msg", "login");
			mv.setViewName("redirect:/index.html");
		}
		return mv;
	}
	/*修改某一篇文章*/
	@RequestMapping(value="userinfo/compileArticle",method=RequestMethod.GET)
	public ModelAndView compileArticle(Integer articleId) {
		ModelAndView mv = new ModelAndView();
		
		Article article = articleService.findByArticleId(articleId);
		Rubbish rubbish = rubbishService.findById(article.getRubbish().getId());
		mv.addObject("article", article);
		mv.addObject("rubbish", rubbish);
		return mv;
	}
	/*提交修改后的内容*/
	@RequestMapping(value="userinfo/compileArticle",method=RequestMethod.POST)
	public String submitCompileArticle(Article article) {
		articleService.updateArticle(article);
		
		return "redirect:/web/userinfo/articleList";
	}
	/*删除文章*/
	@RequestMapping("article/deleteArticle")
	@ResponseBody
	public void deleteArticle(Integer articleId) {
		
		articleService.deleteArticle(articleId);
	
	}
	
	
	/*下拉列表框搜索*/
	@RequestMapping("article/chaXun")
	public  ModelAndView chaXun(Integer rubbishId){
		ModelAndView mv = new ModelAndView();
		Rubbish rubbish = rubbishService.selectById(rubbishId);
		PageInfo<Article> pageInfo =  articleService.findAllByRubbishId(rubbishId);
		List<Firsttype> firsttypes = firsttypeService.findAll();
		List<Firsttype> s1 = new ArrayList<>();
		for (Firsttype firsttype : firsttypes) {
			if (firsttype.getSecondtypes().size()>0) {
				s1.add(firsttype);
			}
		}
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("firsttypes", s1);
		
		mv.addObject("rubbishId", rubbishId);
		mv.addObject("rubbish", rubbish);
		mv.setViewName("/web/article/list");
		return mv;
	}
	
	

	@RequestMapping("article/listByTime")
	public ModelAndView listByTime()
	{
		
		ModelAndView mv = new ModelAndView();
		List<Firsttype> firsttypes = firsttypeService.findAll();
		mv.addObject("firsttypes", firsttypes);
		PageInfo<Article> pageInfo= articleService.findAllByPageAndTime();
		mv.addObject("pageInfo",pageInfo);
		mv.setViewName("/web/article/list");
		return mv;
	}
	
	@RequestMapping("article/reDu")
	public ModelAndView reDu()
	{
		
		ModelAndView mv = new ModelAndView();
		List<Firsttype> firsttypes = firsttypeService.findAll();
		mv.addObject("firsttypes", firsttypes);
		PageInfo<Article> pageInfo= articleService.findAllByPageAndRedu();
		mv.addObject("pageInfo",pageInfo);
		mv.setViewName("/web/article/list");
		return mv;
	}

	/*某个用户收藏的文章*/
	@RequestMapping("/userinfo/myArticleCollect")
	public ModelAndView collectArticle(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		HttpSession session = request.getSession();
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		if(userinfo==null) {
			mv.addObject("msg", "login");
			mv.setViewName("redirect:/index.html");
		}else {
			PageInfo<Article> pageInfo = null;
			
			pageInfo = articleService.findCollectArticle(userinfo.getId());
			mv.addObject("pageInfo", pageInfo);
		}
		
		return mv;
	}
	
	
	
	
	
	
	
}
